package per.hx.bs.web.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.entity.sys.SysRoleDO;
import per.hx.bs.web.sys.service.SysRoleService;

import java.util.List;

/**
 * @author HuXing
 * @date 2018/3/22  19:41
 */
@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    private final String prefix = "system/role/";

    private final
    SysRoleService sysRoleService;

    @Autowired
    public RoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @RequiresPermissions("sys:role:role")
    @GetMapping(value = {"/", ""})
    public String role() {
        return prefix + "role";
    }

    @RequiresPermissions("sys:role:role")
    @GetMapping("/list")
    @ResponseBody()
    public List<SysRoleDO> list() {
        return sysRoleService.list();
    }

    @BsCLog("添加角色")
    @RequiresPermissions("sys:role:add")
    @GetMapping(value = "/add")
    public String add() {
        return prefix + "add";
    }

    @BsCLog("编辑角色")
    @RequiresPermissions("sys:role:edit")
    @GetMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        SysRoleDO sysRoleDO = sysRoleService.getOne(id);
        model.addAttribute("role", sysRoleDO);
        return prefix + "/edit";
    }

    @BsCLog("保存角色")
    @RequiresPermissions("sys:role:add")
    @PostMapping(value = "/save")
    @ResponseBody()
    public ReturnDTO save(SysRoleDO role) {

        if (sysRoleService.save(role) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error(1, "保存失败");
        }
    }

    @BsCLog("更新角色")
    @RequiresPermissions("sys:role:edit")
    @PostMapping(value = "/update")
    @ResponseBody()
    public ReturnDTO update(SysRoleDO role) {

        if (sysRoleService.update(role) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error(1, "保存失败");
        }
    }

    @BsCLog("删除角色")
    @RequiresPermissions("sys:role:remove")
    @PostMapping(value = "/remove")
    @ResponseBody()
    public ReturnDTO save(Long id) {

        if (sysRoleService.remove(id) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error(1, "删除失败");
        }
    }

    @BsCLog("批量删除角色")
    @RequiresPermissions("sys:role:batchRemove")
    @PostMapping(value = "/batchRemove")
    @ResponseBody
    public ReturnDTO batchRemove(@RequestParam("ids[]") Long[] ids) {

        int r = sysRoleService.batchRemove(ids);
        if (r > 0) {
            return ReturnDTO.ok();
        }
        return ReturnDTO.error(1, "批量删除失败");
    }

    @BsCLog("检查标识符是否存在")
    @RequiresPermissions("sys:role:role")
    @PostMapping(value = "/roleSign")
    @ResponseBody
    public Boolean signExit(@RequestParam String roleSign) {
        return sysRoleService.signExit(roleSign);
    }


}
